package byggemarked;

import Views.*;
import Models.*;
import javax.swing.JOptionPane;

/**
 * @author Thomas, Erik og Morten
 */
public class MainGUI extends javax.swing.JFrame
{
	Employee currentEmployee = null;

    public MainGUI()
	{
        initComponents();
    }

	private void loginProcedure()
	{
		boolean hasValidId = false;
		String employeeIdStr = "";
		Employee foundEmployee = null;
		while (!hasValidId)
		{
			employeeIdStr = JOptionPane.showInputDialog(this, "Indlæs medarbejderkort", "Indlæs kort", JOptionPane.INFORMATION_MESSAGE);
			if (employeeIdStr == null)
			{ return; }
			try
			{
				int employeeId = Integer.parseInt(employeeIdStr);
				foundEmployee = EmployeeCollection.getInstance().getEmployee(employeeId);
				if (foundEmployee != null)
				{ hasValidId = true; }
				else
				{
					JOptionPane.showMessageDialog(this, "Dette ID findes ikke i databasen.", "ID findes ikke!", JOptionPane.WARNING_MESSAGE);
				}
			}
			catch (NumberFormatException e)
			{ JOptionPane.showMessageDialog(this, "Dit login er ikke gyldigt.", "Ugyldigt login!", JOptionPane.WARNING_MESSAGE);}
		}
		if (foundEmployee != null)
		{
			this.currentEmployee = foundEmployee;
			welcomeLabel.setText(foundEmployee.getName()+" er logget ind.");
			this.enableButtons();
			loginButton.setText("Log af");
		}
	}

	private void logoutProcedure()
	{
		this.currentEmployee = null;
		welcomeLabel.setText("Der er ingen medarbejdere logget ind.");
		this.disableButtons();
		loginButton.setText("Log ind");
	}

	private void disableButtons()
	{
		saleButton.setEnabled(false);
		rentalButton.setEnabled(false);
		customerButton.setEnabled(false);
		stockButton.setEnabled(false);
		kitchenButton.setEnabled(false);
		machineButton.setEnabled(false);
		supplierButton.setEnabled(false);
		employeeButton.setEnabled(false);
	}

	private void enableButtons()
	{
		saleButton.setEnabled(true);
		rentalButton.setEnabled(true);
		customerButton.setEnabled(true);
		stockButton.setEnabled(true);
		kitchenButton.setEnabled(true);
		machineButton.setEnabled(true);
		supplierButton.setEnabled(true);
		employeeButton.setEnabled(true);
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginButton = new javax.swing.JButton();
        welcomeLabel = new javax.swing.JLabel();
        saleButton = new javax.swing.JButton();
        rentalButton = new javax.swing.JButton();
        customerButton = new javax.swing.JButton();
        stockButton = new javax.swing.JButton();
        kitchenButton = new javax.swing.JButton();
        machineButton = new javax.swing.JButton();
        supplierButton = new javax.swing.JButton();
        employeeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Hovedvindue"); // NOI18N
        setResizable(false);

        loginButton.setText("Log ind");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        welcomeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        welcomeLabel.setText("Der er ingen ansatte logget ind.");

        saleButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        saleButton.setText("Salg");
        saleButton.setEnabled(false);
        saleButton.setMaximumSize(new java.awt.Dimension(150, 150));
        saleButton.setMinimumSize(new java.awt.Dimension(150, 150));
        saleButton.setPreferredSize(new java.awt.Dimension(140, 75));
        saleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saleButtonActionPerformed(evt);
            }
        });

        rentalButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        rentalButton.setText("Udlejning");
        rentalButton.setEnabled(false);
        rentalButton.setMaximumSize(new java.awt.Dimension(150, 150));
        rentalButton.setMinimumSize(new java.awt.Dimension(150, 150));
        rentalButton.setPreferredSize(new java.awt.Dimension(140, 75));
        rentalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rentalButtonActionPerformed(evt);
            }
        });

        customerButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        customerButton.setText("Kunder");
        customerButton.setEnabled(false);
        customerButton.setMaximumSize(new java.awt.Dimension(150, 150));
        customerButton.setMinimumSize(new java.awt.Dimension(150, 150));
        customerButton.setPreferredSize(new java.awt.Dimension(140, 75));
        customerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                customerButtonActionPerformed(evt);
            }
        });

        stockButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        stockButton.setText("Lager");
        stockButton.setEnabled(false);
        stockButton.setMaximumSize(new java.awt.Dimension(150, 150));
        stockButton.setMinimumSize(new java.awt.Dimension(150, 150));
        stockButton.setPreferredSize(new java.awt.Dimension(140, 75));
        stockButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stockButtonActionPerformed(evt);
            }
        });

        kitchenButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        kitchenButton.setText("Køkkener");
        kitchenButton.setEnabled(false);
        kitchenButton.setMaximumSize(new java.awt.Dimension(150, 150));
        kitchenButton.setMinimumSize(new java.awt.Dimension(150, 150));
        kitchenButton.setPreferredSize(new java.awt.Dimension(140, 75));
        kitchenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kitchenButtonActionPerformed(evt);
            }
        });

        machineButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        machineButton.setText("Maskiner");
        machineButton.setEnabled(false);
        machineButton.setMaximumSize(new java.awt.Dimension(150, 150));
        machineButton.setMinimumSize(new java.awt.Dimension(150, 150));
        machineButton.setPreferredSize(new java.awt.Dimension(140, 75));
        machineButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                machineButtonActionPerformed(evt);
            }
        });

        supplierButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        supplierButton.setText("Leverandører");
        supplierButton.setEnabled(false);
        supplierButton.setMaximumSize(new java.awt.Dimension(150, 150));
        supplierButton.setMinimumSize(new java.awt.Dimension(150, 150));
        supplierButton.setPreferredSize(new java.awt.Dimension(140, 75));
        supplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supplierButtonActionPerformed(evt);
            }
        });

        employeeButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        employeeButton.setText("Ansatte");
        employeeButton.setEnabled(false);
        employeeButton.setMaximumSize(new java.awt.Dimension(150, 150));
        employeeButton.setMinimumSize(new java.awt.Dimension(150, 150));
        employeeButton.setPreferredSize(new java.awt.Dimension(140, 75));
        employeeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                employeeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(welcomeLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(saleButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(rentalButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(customerButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(stockButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(kitchenButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(machineButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(supplierButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(employeeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .add(loginButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 610, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(loginButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(welcomeLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(saleButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(rentalButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(customerButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(stockButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(kitchenButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(machineButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(supplierButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(employeeButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void saleButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saleButtonActionPerformed
	{//GEN-HEADEREND:event_saleButtonActionPerformed
		new SaleView(this.currentEmployee).setVisible(true);
}//GEN-LAST:event_saleButtonActionPerformed

	private void rentalButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_rentalButtonActionPerformed
	{//GEN-HEADEREND:event_rentalButtonActionPerformed
		new RentalView().setVisible(true);
	}//GEN-LAST:event_rentalButtonActionPerformed

	private void stockButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_stockButtonActionPerformed
	{//GEN-HEADEREND:event_stockButtonActionPerformed
		new ItemView().setVisible(true);
	}//GEN-LAST:event_stockButtonActionPerformed

	private void customerButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_customerButtonActionPerformed
	{//GEN-HEADEREND:event_customerButtonActionPerformed
		new CustomerView().setVisible(true);
	}//GEN-LAST:event_customerButtonActionPerformed

	private void machineButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_machineButtonActionPerformed
	{//GEN-HEADEREND:event_machineButtonActionPerformed
		new MachineView().setVisible(true);
	}//GEN-LAST:event_machineButtonActionPerformed

	private void kitchenButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_kitchenButtonActionPerformed
	{//GEN-HEADEREND:event_kitchenButtonActionPerformed
		new KitchenView().setVisible(true);
	}//GEN-LAST:event_kitchenButtonActionPerformed

	private void supplierButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_supplierButtonActionPerformed
	{//GEN-HEADEREND:event_supplierButtonActionPerformed
		new SupplierView().setVisible(true);
	}//GEN-LAST:event_supplierButtonActionPerformed

	private void employeeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_employeeButtonActionPerformed
	{//GEN-HEADEREND:event_employeeButtonActionPerformed
		new EmployeeView().setVisible(true);
	}//GEN-LAST:event_employeeButtonActionPerformed

	private void loginButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_loginButtonActionPerformed
	{//GEN-HEADEREND:event_loginButtonActionPerformed
		if (currentEmployee == null)
		{
			this.loginProcedure();
		}
		else
		{
			this.logoutProcedure();
		}
	}//GEN-LAST:event_loginButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton customerButton;
    private javax.swing.JButton employeeButton;
    private javax.swing.JButton kitchenButton;
    private javax.swing.JButton loginButton;
    private javax.swing.JButton machineButton;
    private javax.swing.JButton rentalButton;
    private javax.swing.JButton saleButton;
    private javax.swing.JButton stockButton;
    private javax.swing.JButton supplierButton;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

}
